package com.bjtime.designer.widget

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.AttributeSet
import android.util.Base64
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.AbsoluteLayout
import android.widget.ImageView
import android.widget.TextView
import com.bjtime.designer.QRCode
import com.bjtime.designer.R
import com.bjtime.designer.attributes.*
import com.bjtime.designer.utils
import java.io.ByteArrayOutputStream


class QRCodeView2 : BaseLabelView , View.OnTouchListener {

    var inflater : LayoutInflater
    var mLayoutView : View
    var mBorderView : View
    var mWBtn       : View
    var mHBtn       : View? = null

    val TagViewName = "QRCodeView2"
    var des_text    : String? = "9527666"
    var QRImage : ImageView? = null
    var des_image: Bitmap? = null
    var des_scale : Int = 0
    var des_type  : QRCodeAttr.QRTYPE = QRCodeAttr.QRTYPE.QR_CODE



    @JvmOverloads
    constructor(context: Context, content : String,attrs: AttributeSet? = null, defStyleAttr: Int = 0) : super(context, attrs, defStyleAttr) {
        des_viewtype = ViewAttr.VIEW_TYPE.QRCODE
        des_text=content
        inflater = LayoutInflater.from(this.context)
        mLayoutView = inflater.inflate(R.layout.layout_designer_label_qrcodeview2, null)
        this.addView(mLayoutView)
        QRImage = mLayoutView.findViewById(R.id.qrcodeview2_label_qrimageview)
        mBorderView = mLayoutView.findViewById<View>(R.id.qrcodeview2_label_broderview)
        mWBtn = mLayoutView.findViewById(R.id.qrcodeview2_label_right_btn)
        //mHBtn = mLayoutView.findViewById(R.id.qrcodeview2_label_bottom_btn)
        des_image = QRCode.createQRCode(des_text!!, 600, 600, null, des_type)
        QRImage!!.setImageBitmap(des_image)

        val a = context.obtainStyledAttributes(attrs, R.styleable.QRCodeView2)
        des_text = if (a.getText(R.styleable.QRCodeView2_text) == null)des_text else a.getText(R.styleable.QRCodeView2_text).toString()
        a.recycle()

        //mHBtn.setOnTouchListener(this)
        mWBtn.setOnTouchListener(this)

        setSelect (false)
    }


    private var xDelta: Float = 0f
    private var yDelta: Float = 0f

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                //xDelta = x - getWidth()
                //yDelta = y - getHeight()
                requestDisallowInterceptTouchEvent(true)
            }

            MotionEvent.ACTION_MOVE -> {
                xDelta = width.toFloat()
                yDelta = height.toFloat()
                val params = (this.layoutParams as AbsoluteLayout.LayoutParams)
                val xDistance = x + xDelta
                val yDistance = y + yDelta
                params.width = xDistance.toInt()
                params.height = xDistance.toInt()
                this.layoutParams = params
            }

            MotionEvent.ACTION_UP ->{
                requestDisallowInterceptTouchEvent(false)
            }
        }
        return true
    }


    fun setText (s : String?, isUpdate : Boolean = true) {
        if (s != null || !s.equals(""))
            des_text = s

        if (isUpdate) {
            des_image = QRCode.createQRCode(des_text!!, 600, 600, null, des_type)
            QRImage!!.setImageBitmap(des_image)
        }
    }



    override fun setSelect(select: Boolean) {
        super.setSelect(select)
        if (!select) {
            mBorderView.setBackgroundResource(0)
            mWBtn.visibility = View.GONE
        } else {
            if (!des_lock) {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border)
                mWBtn.visibility = View.VISIBLE
            } else {
                mBorderView.setBackgroundResource(R.drawable.shape_designer_label_border_locked)
                mWBtn.visibility = View.GONE
            }
        }
    }

    override fun getAttr (): QRCodeAttr {
        var attr  = QRCodeAttr ()
        attr.des_id = des_id
        attr.des_left = des_left
        attr.des_top = des_top
        attr.des_width = des_width
        attr.des_height = des_height
        attr.des_lock = des_lock
        attr.des_ptype = des_ptype
        attr.des_rotate = des_rotate
        attr.des_text   = des_text!!

        attr.des_image  = utils.Bitmap2ByteArray (des_image!!)
        attr.des_viewtype = ViewAttr.VIEW_TYPE.QRCODE

        attr.prt_bottom = prt_bottom
        attr.prt_right  = prt_right
        attr.prt_top    = prt_top
        attr.prt_left   = prt_left

        attr.des_type   = des_type
        return attr
    }

    override fun setAttr (attr: BaseAttr?) {
        attr as QRCodeAttr
        des_id = attr.des_id
        des_left = attr.des_left
        des_top = attr.des_top
        des_width = attr.des_width
        des_height = attr.des_height
        des_lock = attr.des_lock
        des_ptype = attr.des_ptype
        des_rotate = attr.des_rotate
        des_reversed = attr.des_reversed
        des_viewtype = attr.des_viewtype
        des_image =  BitmapFactory.decodeByteArray(attr.des_image, 0, attr.des_image!!.size)
        des_scale = attr.des_scale
        des_text = attr.des_text

        prt_bottom = attr.prt_bottom
        prt_right = attr.prt_right
        prt_top = attr.prt_top
        prt_left = attr.prt_left

        des_type   = attr.des_type
        setImage (attr.des_image!!)
        setText(attr.des_text, false)
        setType (des_type)

    }

    override fun toString(): String {
        val s = "ViewName    : " + TagViewName  + "\n" +
                "view_id     : " + des_id       + "\n" +
                "view_left   : " + des_left     + "\n" +
                "view_top    : " + des_top      + "\n" +
                "view_width  : " + des_width    + "\n" +
                "view_height : " + des_height   + "\n" +
                "view_islock : " + des_lock     + "\n" +
                "view_select : " + des_select   + "\n" +
                "view_ptype  : " + des_ptype    + "\n" +
                "view_rotate : " + des_rotate   + "\n"
        return s
    }

    override fun onMoved(spac: Int) {
        super.onMoved(spac)
        prt_left = (des_left + utils.dp2px(context, 11f)) / spac
        prt_top = (des_top + utils.dp2px(context, 11f)) / spac
        prt_right = (des_width - utils.dp2px(context, 22f)) / spac
        prt_bottom = (des_height - utils.dp2px(context, 22f)) / spac
    }


    fun setImage (img : ByteArray) {
        des_image = BitmapFactory.decodeByteArray(img, 0, img.size)
        QRImage!!.setImageBitmap(des_image)
    }


    fun setType (type : QRCodeAttr.QRTYPE) {
        des_type = type
        des_image = QRCode.createQRCode(des_text!!, 600, 600, null, des_type)
        QRImage!!.setImageBitmap(des_image)
    }


    override fun prepared() {
        setText(des_text)
    }
}
